# -*- coding:utf-8 -*-
# @author='dapeng',
# @version='1.0.0'
import requests,os,random

#下载图片,返回本地存储的文件位置
def download_image(url, path, ref, separator):
    headers = {
        "Referer": ref,
        "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
    }
    file_name = str(url).split(separator)[-1]
    response = requests.get(url, headers=headers)
    if response.status_code != 200:
        pass
    else:
        file_path = path + str(file_name)
        with open(file_path, mode='wb') as f:
            f.write(response.content)
            return file_path


# 创建目录
def create_folder(path):
    if not os.path.exists(path):
        os.makedirs(path)
    else:
        pass


#下载必应壁纸返回文件路径
def getbgp():
    id = random.randint(1, 6)
    img = requests.get('https://bing.biturl.top/?resolution=1920&format=image&index=%s&mkt=zh-CN' % id)
    basepath ='D:/meizitu/backgroundimg/'
    create_folder(basepath)
    path = download_image(img.url, basepath,'https://www.bing.com','=')
    return path


#返回下载二次元图片返回路径
def erciyuan():
    url = 'https://api.vvhan.com/api/acgimg'
    params = {'type': 'json'}
    res = requests.get(url, params=params).json()
    basepath ='D:/meizitu/erciyuan/'
    create_folder(basepath)
    path = download_image(res.get('imgurl'), basepath,url, '/')
    print(path)
    return path

